package com.example.x6.configurationmaintenance.DataBase.ServiceDaemon;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.app.NotificationCompat;
import com.example.x6.configurationmaintenance.Interface.ServiceDaemonDatabase.OnServiceDaemonDatabaseChangeListener;
import com.example.x6.configurationmaintenance.Interface.ServiceDaemonDatabase.OnServiceDaemonDatabaseErgodicListener;
import com.example.x6.configurationmaintenance.Utils.SuCommand;
import java.util.Vector;

public class ServiceDaemonDatabaseOperation {
    public static final int DATA_NOTEXISTED = -3;
    public static final int SERVICE_EXISTED = -2;
    public static final int SERVICE_FULL = -4;
    public static final int SERVICE_INVALID = -1;
    public static final int SERVICE_NOTEXISTED = -5;
    public static final int SUCCESS = 0;
    private static ServiceDaemonDatabaseOperation serviceDaemonDatabaseOperation;
    private final int MAX_SERVICE = 18;
    private final String TAG = getClass().getSimpleName();
    private Context context;
    private OnServiceDaemonDatabaseChangeListener onServiceDaemonDatabaseChangeListener;
    private OnServiceDaemonDatabaseErgodicListener onServiceDaemonDatabaseErgodicListener;
    private ServiceDaemonDatabaseManager serviceDaemonDatabaseManager;
    private String[] serviceList;
    private SuCommand suCommand;

    public static synchronized void initializeInstance(Context context2) {
        synchronized (ServiceDaemonDatabaseOperation.class) {
            if (serviceDaemonDatabaseOperation == null) {
                serviceDaemonDatabaseOperation = new ServiceDaemonDatabaseOperation(context2);
            }
        }
    }

    public static synchronized ServiceDaemonDatabaseOperation getInstance() {
        ServiceDaemonDatabaseOperation serviceDaemonDatabaseOperation2;
        synchronized (ServiceDaemonDatabaseOperation.class) {
            if (serviceDaemonDatabaseOperation == null) {
                throw new IllegalStateException("DatabaseOperation is not initalized ,call initializeInstance(Context context) first");
            }
            serviceDaemonDatabaseOperation2 = serviceDaemonDatabaseOperation;
        }
        return serviceDaemonDatabaseOperation2;
    }

    private ServiceDaemonDatabaseOperation(Context context2) {
        this.context = context2;
        ServiceDaemonDatabaseManager.initializeInstance(new ServiceDaemonDatabaseHelper(this.context));
        this.serviceDaemonDatabaseManager = ServiceDaemonDatabaseManager.getInstance();
        this.suCommand = new SuCommand();
    }

    public void setOnServiceDaemonDatabaseChangeListener(OnServiceDaemonDatabaseChangeListener onServiceDaemonDatabaseChangeListener2) {
        this.onServiceDaemonDatabaseChangeListener = onServiceDaemonDatabaseChangeListener2;
        flushServiceList();
    }

    public void setOnServiceDaemonDatabaseErgodicListener(OnServiceDaemonDatabaseErgodicListener onServiceDaemonDatabaseErgodicListener2) {
        this.onServiceDaemonDatabaseErgodicListener = onServiceDaemonDatabaseErgodicListener2;
        getServiceDaemonList();
    }

    public void getServiceDaemonList() {
        new Thread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.DataBase.ServiceDaemon.ServiceDaemonDatabaseOperation.AnonymousClass1 */

            public void run() {
                Cursor cursor = ServiceDaemonDatabaseOperation.this.serviceDaemonDatabaseManager.getReadableDatabase().rawQuery("select * from serviceDaemon", new String[0]);
                if (cursor.getCount() == 0) {
                    ServiceDaemonDatabaseOperation.this.onServiceDaemonDatabaseErgodicListener.serviceDaemonDatabaseErgodicFinished(true);
                    ServiceDaemonDatabaseOperation.this.serviceDaemonDatabaseManager.closeDatabase();
                    return;
                }
                while (cursor.moveToNext()) {
                    ServiceDaemonDatabaseOperation.this.onServiceDaemonDatabaseErgodicListener.serviceDaemonDatabaseErgodic(cursor.getString(cursor.getColumnIndex(NotificationCompat.CATEGORY_SERVICE)));
                }
                ServiceDaemonDatabaseOperation.this.onServiceDaemonDatabaseErgodicListener.serviceDaemonDatabaseErgodicFinished(false);
                ServiceDaemonDatabaseOperation.this.serviceDaemonDatabaseManager.closeDatabase();
            }
        }).start();
    }

    public synchronized int insert(String service) {
        if (service != null) {
            if (!service.isEmpty()) {
                if (!isServiceCanStart(service)) {
                    return -5;
                }
                String[] times = new String[0];
                if (this.serviceDaemonDatabaseManager.getReadableDatabase().rawQuery("select * from serviceDaemon", times).getCount() >= 18) {
                    this.serviceDaemonDatabaseManager.closeDatabase();
                    return -4;
                }
                if (this.serviceDaemonDatabaseManager.getReadableDatabase().rawQuery("select * from serviceDaemon WHERE service='" + service + "'", times).getCount() > 0) {
                    this.serviceDaemonDatabaseManager.closeDatabase();
                    return -2;
                }
                this.serviceDaemonDatabaseManager.getWritableDatabase().execSQL("insert into serviceDaemon (service) values('" + service + "')");
                this.serviceDaemonDatabaseManager.closeDatabase();
                flushServiceList();
                return 0;
            }
        }
        return -1;
    }

    public synchronized int delete(String service) {
        if (service != null) {
            if (!service.isEmpty()) {
                if (this.serviceDaemonDatabaseManager.getReadableDatabase().rawQuery("select * from serviceDaemon WHERE service='" + service + "'", new String[0]).getCount() == 0) {
                    this.serviceDaemonDatabaseManager.closeDatabase();
                    return -3;
                }
                this.serviceDaemonDatabaseManager.getWritableDatabase().execSQL("delete from serviceDaemon WHERE service='" + service + "'");
                this.serviceDaemonDatabaseManager.closeDatabase();
                flushServiceList();
                return 0;
            }
        }
        return -1;
    }

    public String[] getSerivceList() {
        new Vector();
        Cursor cursor = this.serviceDaemonDatabaseManager.getReadableDatabase().rawQuery("select * from serviceDaemon", new String[0]);
        String[] result = new String[cursor.getCount()];
        while (cursor.moveToNext()) {
            result[cursor.getPosition()] = cursor.getString(cursor.getColumnIndex(NotificationCompat.CATEGORY_SERVICE));
        }
        this.serviceDaemonDatabaseManager.closeDatabase();
        return result;
    }

    private void flushServiceList() {
        new Thread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.DataBase.ServiceDaemon.ServiceDaemonDatabaseOperation.AnonymousClass2 */

            public void run() {
                ServiceDaemonDatabaseOperation.this.serviceList = ServiceDaemonDatabaseOperation.this.getSerivceList();
                if (ServiceDaemonDatabaseOperation.this.onServiceDaemonDatabaseChangeListener != null) {
                    ServiceDaemonDatabaseOperation.this.onServiceDaemonDatabaseChangeListener.onServiceDaemonDataBaseChange(ServiceDaemonDatabaseOperation.this.serviceList);
                }
            }
        }).start();
    }

    private boolean isServiceCanStart(String service) {
        if (this.suCommand.execRootCmdSilent("am startservice -n " + service) == 0) {
            return true;
        }
        return false;
    }
}
